/*
 * Copyright (c) Thorben Linneweber and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using Jitter2.LinearMath;

using SoftFloat;

namespace Jitter2.Collision.Shapes
{
	
	/// <summary>
	/// Represents a sphere.
	/// </summary>
	public class SphereShape : RigidBodyShape
	{
	    private sfloat radius;
	
	    /// <summary>
	    /// Gets or sets the radius of the sphere.
	    /// </summary>
	    public sfloat Radius
	    {
	        get => radius;
	        set
	        {
	            radius = value;
	            UpdateWorldBoundingBox(sfloat.Zero);
	        }
	    }
	
	    /// <summary>
	    /// Initializes a new instance of the <see cref="SphereShape"/> class with an optional radius parameter.
	    /// The default radius is 1.0 units.
	    /// </summary>
	    /// <param name="radius">The radius of the sphere. Defaults to (sfloat)1.0f.</param>
	    public SphereShape(sfloat radius /*= (sfloat)1.0f*/)
	    {
	        this.radius = radius;
	        UpdateWorldBoundingBox(sfloat.Zero);
	    }
	
	    public override void SupportMap(in NVector3 direction, out NVector3 result)
	    {
	        result = direction;
	        result.Normalize();
	        NVector3.Multiply(result, radius, out result);
	    
	        result += centerOffset;// 应用中心偏移
		}
	
	    public override void GetCenter(out NVector3 point)
	    {
	        point = centerOffset;
	    }
	
	    public override void CalculateBoundingBox(in NQuaternion orientation, in NVector3 position, out JBBox box)
	    {
	        box.Min.X = -radius;
	        box.Min.Y = -radius;
	        box.Min.Z = -radius;
	        box.Max.X = +radius;
	        box.Max.Y = +radius;
	        box.Max.Z = +radius;
	
	        NVector3.Add(box.Min, position, out box.Min);
	        NVector3.Add(box.Max, position, out box.Max);
	    
	        // 考虑位置和中心偏移
	        box.Min += centerOffset;
	        box.Max += centerOffset;
		}
	
	    public override bool LocalRayCast(in NVector3 origin, in NVector3 direction, out NVector3 normal, out sfloat lambda)
	    {
	        normal = NVector3.Zero;
	        lambda = (sfloat)0.0f;
	
	        sfloat disq = (sfloat)1.0f / direction.LengthSquared();
	        sfloat p = NVector3.Dot(direction, origin) * disq;
	        sfloat d = p * p - (origin.LengthSquared() - radius * radius) * disq;
	
	        if (d < (sfloat)0.0f) return false;
	
	        sfloat sqrtd = libm.Sqrt(d);
	
	        sfloat t0 = -p - sqrtd;
	        sfloat t1 = -p + sqrtd;
	
	        if (t0 >= (sfloat)0.0f)
	        {
	            lambda = t0;
	            NVector3.Normalize(origin + t0 * direction, out normal);
	            return true;
	        }
	
	        return t1 > (sfloat)0.0f;
	    }
	
	    public override void CalculateMassInertia(out JMatrix inertia, out NVector3 com, out sfloat mass)
	    {
	        mass = (sfloat)4.0f / (sfloat)3.0f * libm.PI * radius * radius * radius;
	
	        // (0,0,0) is the center of mass
	        inertia = JMatrix.Identity;
	        inertia.M11 = (sfloat)2.0f / (sfloat)5.0f * mass * radius * radius;
	        inertia.M22 = (sfloat)2.0f / (sfloat)5.0f * mass * radius * radius;
	        inertia.M33 = (sfloat)2.0f / (sfloat)5.0f * mass * radius * radius;
	
	        com = NVector3.Zero;
	    }
	}
}
